# 读取数据
import pandas as pd
data = pd.read_csv('scatter_data.csv')

# 给x和y赋值
x = data.drop(['label'], axis=1)
y = data.loc[:,'label']

# 分离出训练集和测试集
from sklearn.model_selection import train_test_split
x, x_test, y, y_test = train_test_split(x, y, test_size=0.3, random_state=10)

# 建立一个Sequential顺序模型
from keras.models import Sequential
model = Sequential()

# 通过add叠加各层网络
from keras.layers import Dense
model.add(Dense(units=20, activation='sigmoid', input_dim=2)) # 第一层（输入层）到第二层（隐藏层）
model.add(Dense(units=1, activation='sigmoid')) # 第二层（隐藏层）到第三层（输出层）

# 查看模型结构
model.summary()

# 通过.compile()配置模型求解过程参数
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# 训练模型
model.fit(x, y, epochs=3000)

# 预测值
y_predict = model.predict(x_test)

# 将概率值转换成离散的类别标签
y_predict_classes = (y_predict > 0.5).astype(int)

# 计算准确率
from sklearn.metrics import accuracy_score

print(accuracy_score(y_test, y_predict_classes))